import codecs



# dev
input_path = './data/result_dev.scp'

def load_dict_from_scp(label_scp_file: str) -> dict:
    """
    得到scp文件的内容,要求key value以空格分割， 第一个为key,剩下的都是value。
    :param label_scp_file:
    :return:
    """
    res = {}
    with codecs.open(label_scp_file, 'r', encoding='utf-8') as f:
        try:
            lines = f.readlines()
        except Exception as e:
            print(e)
            return {}
        for line in lines:
            line = line.strip()
            items = line.split()
            if len(items) < 2:
                print(
                    'load_dict_from_scp;warning_gxl:, this row not conform to the regulation of scp(key content) and skip it:',
                    line)
                continue
            elif len(items) == 2:
                res[items[0].strip()] = items[1].strip()
            else:
                # logging_print(
                #     'warning_gxl:, this row not conform to the regulation of'
                #     ' scp(key content) and no skip it,第一个为key,剩下的都是value:',
                #     line)
                res[items[0].strip()] = (' '.join(items[1:])).strip()
    total_len = len(res)
    print("load_dict_from_scp()_数据总条数为:", total_len)
    return res
res_dict = load_dict_from_scp(input_path)
res_tuple_list = []
res_relitu_dict = {}
for value in res_dict.values():
    item_1 = value[1] # predict
    item_2 = value[-2] # label
    if item_2 not in res_relitu_dict:
        res_relitu_dict[str(item_2)] = {'total':0,'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,'7':0}
    else:
        res_relitu_dict[str(item_2)]['total'] += 1
        res_relitu_dict[str(item_2)][str(item_1)] += 1
    res_tuple_list.append((item_1, item_2))
print(res_tuple_list)
relitu_matrix = []
for i in range(8):
    if str(i) not in res_relitu_dict:
        total = 0.000001
        res_relitu_dict[str(i)] = {'total':0,'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,'7':0}
    else:
        total = res_relitu_dict[str(i)]['total']
    print(f'{str(i)}: {total}')
    relitu_matrix.append([])
    for j in range(8):
        now_num = res_relitu_dict[str(i)][str(j)]
        relitu_matrix[i].append(now_num/total)

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 8))
sns.heatmap(np.array(relitu_matrix), annot=True, cmap="YlGnBu")
plt.show()